<?php

	class ProductsImagesController extends AppController {
		var $name = 'ProductsImages';
		var $uses = array('ProductImage');
		var $components = array('UploadFile');
		
		var $uploadPath;
		var $uploadReturn = array();
		
		function beforeFilter() {
			parent::beforeFilter();
			//Configure::write('debug', 1);
			$this->autoRender = false;
			$this->uploadPath = ROOT . DS . 'tmp' . DS . Configure::read('Shop.alias');
		}
		
		function dashboard_preview() {
			$request = array();
			
			if ($file = !empty($this->params['url']['qqfile']) ? $this->params['url']['qqfile'] : (!empty($this->params['form']['qqfile']) ? $this->params['form']['file'] : null)) {
				$count_images = 0;
				if($this->Session->check('Product.Images')) {
					$count_images = count($this->Session->read('Product.Images'));
				}
				
				$request+= $result = $this->UploadFile->handle($file, array('path' => $this->uploadPath, 'createPath' => true, 'fileName' => time()));
				
				$fileinfo = pathinfo($result['file']);
				
				$request+= array(
					'id' => $count_images,
					'hash' => Security::hash($count_images),
					'path' => $this->uploadPath,
					'url' => Configure::read('Site.Url.main') . '/tmp/' . Configure::read('Shop.alias') . '/' . $fileinfo['filename'] . '_medium.' . $fileinfo['extension']
				);
				
				App::import('Vendor', 'Thumbnail');
				$thumbnail = new Thumbnail;
				$thumbnail->resize($this->uploadPath . DS . $request['file'], $this->uploadPath . DS . $fileinfo['filename'] . '_pico.' . $fileinfo['extension'], 16);
				$thumbnail->resize($this->uploadPath . DS . $request['file'], $this->uploadPath . DS . $fileinfo['filename'] . '_icon.' . $fileinfo['extension'], 32);
				$thumbnail->resize($this->uploadPath . DS . $request['file'], $this->uploadPath . DS . $fileinfo['filename'] . '_tiny.' . $fileinfo['extension'], 50);
				$thumbnail->resize($this->uploadPath . DS . $request['file'], $this->uploadPath . DS . $fileinfo['filename'] . '_small.' . $fileinfo['extension'], 100);
				$thumbnail->resize($this->uploadPath . DS . $request['file'], $this->uploadPath . DS . $fileinfo['filename'] . '_compact.' . $fileinfo['extension'], 160);
				$thumbnail->resize($this->uploadPath . DS . $request['file'], $this->uploadPath . DS . $fileinfo['filename'] . '_medium.' . $fileinfo['extension'], 240);
				$thumbnail->resize($this->uploadPath . DS . $request['file'], $this->uploadPath . DS . $fileinfo['filename'] . '_large.' . $fileinfo['extension'], 480);
				$thumbnail->resize($this->uploadPath . DS . $request['file'], $this->uploadPath . DS . $fileinfo['filename'] . '_grande.' . $fileinfo['extension'], 600);
				
				$this->Session->write('Product.Images.'.$count_images, $request);
				$count_images++;
			}
			
			return htmlspecialchars(json_encode($request), ENT_NOQUOTES);
			
			/*
			App::import('Vendor', 'FileUploader');
			
			// list of valid extensions, ex. array("jpeg", "xml", "bmp")
			$allowedExtensions = array();
			// max file size in bytes
			$sizeLimit = 10 * 1024 * 1024;
			
			$uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
			$result = $uploader->handleUpload($this->pathUpload);
			// to pass data through iframe you will need to encode all html tags
			echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
			exit;
			
			$request = array();
			if (!empty($this->data)) {
				
				$count_images = 0;
				if($this->Session->check('Product.Images')) {
					$count_images = count($this->Session->read('Product.Images'));
				}
				
				$path = ROOT . DS . 'tmp' . DS . $this->Session->read('Auth.Shop.slug');
				foreach($this->data['ProductImage']['preview'] AS $preview) {
					$upload = $this->uploadFile($preview,
						array(
							'name' => time(),
							'path' => $path,
							'createPath' => true
						)
					);
					if (is_object($upload)) {
						$request[] = array(
							'status' => 'error',
							'message' => ''
						);
					} else {
						$__preview = explode('.', $upload);
						
						$image = $request[] = array(
							'status' => 'success',
							'id' => $count_images,
							'idEncrypt' => Security::hash($count_images),
							'name' => $upload,
							'file' => $path . DS . $upload,
							'url' => FULL_BASE_URL . dirname($this->webroot) . '/tmp/' . $this->Session->read('Auth.Shop.slug') . '/' . $__preview[0] . '_preview.' . $__preview[1]
						);
						
						App::import('Vendor', 'thumbnail');
						$thumbnail = new Thumbnail;
						$thumbnail->resize($path . DS . $upload, $path . DS . $__preview[0] . '_tiny.' . $__preview[1], 24);
						$thumbnail->resize($path . DS . $upload, $path . DS . $__preview[0] . '_small.' . $__preview[1], 48);
						$thumbnail->resize($path . DS . $upload, $path . DS . $__preview[0] . '_medium.' . $__preview[1], 75);
						$thumbnail->resize($path . DS . $upload, $path . DS . $__preview[0] . '_preview.' . $__preview[1], 144);
						
						// Marcar la primera imagen como predeterminada para el producto
						if ($count_images == 0) {
							$this->Session->write('Product.Preview', $upload);
						}
						$this->Session->write('Product.Images.'.$count_images, $image);
						$count_images++;
					}
				}
			}
			$this->set('request', json_encode($request));
			$this->render('request');
			*/
		}
		
		function dashboard_base($id = null, $idEncrypt = null, $type = 'product') {
			if (Security::hash($id) != $idEncrypt) {
				$this->redirect($this->referer());
			}
			
			$request = array();
			if ($this->Session->check('Product.Images') && $type == 'sess') {
				$name = $this->Session->read('Product.Images.'.$id.'.name');
				$this->Session->write('Product.Preview', $name);
			}
			
			if ($type == 'product') {
				$data = $this->ProductImage->find(
					'first',
					array(
						'conditions' => array(
							'id' => $id
						),
						'fields' => array(
							'product_id',
							'original'
						),
						'recursive' => -1
					)
				);
				
				$this->data['Product']['id'] = $data['ProductImage']['product_id'];
				$this->data['Product']['preview'] = $data['ProductImage']['original'];

				$this->ProductImage->Product->save($this->data);
			}
			
			$this->set('request', htmlspecialchars(json_encode($request), ENT_NOQUOTES));
			$this->render('request');
		}
		
		function dashboard_delete($id = null, $idEncrypt = null, $type = 'product') {
			if (Security::hash($id) != $idEncrypt) {
				$this->redirect($this->referer());
			}
			
			$request = array('status' => 'success');
			if ($type == 'sess' && $this->Session->check('Product.Images')) {
				$images = $this->Session->read('Product.Images');
				$fileinfo = pathinfo($images[$id]['file']);
				$path = $images[$id]['path'];
				unset($images[$id]);
				if (empty($images)) {
					$this->Session->delete('Product.Images');
				}
				else {
					$this->Session->write('Product.Images', $images);
				}
			}
			else if ($type == 'product' && $image = $this->ProductImage->read(null, $id)) {
				$fileinfo = pathinfo($image['filename']);
				$path = $image['path'];
				$this->ProductImage->delete($id);
			}
			
			foreach(glob($path . DS . $fileinfo['filename'] . '*') AS $filename) {
				@unlink($filename);
			}
			
			return htmlspecialchars(json_encode($request), ENT_NOQUOTES);
		}
		
		private function __move()
		{
			//$this->uploadFile();
		}
		
	}

?>